package com.k.demo.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.GoodOrder;
import com.k.demo.entity.GoodOrderDetail;
import com.k.demo.mapper.GoodOrderMapper;
import com.k.demo.service.BalanceService;
import com.k.demo.service.GoodOrderDetailService;
import com.k.demo.service.GoodOrderService;

@RestController
@RequestMapping("/goodorder")
public class GoodOrderController {
	
	@Autowired
	private GoodOrderService goodOrderService;
	
	@Autowired
	private GoodOrderDetailService goodOrderDetailService;
	
	@Autowired
	private BalanceService balanceService;
	
	@RequestMapping("/all")
	public Object getAllGoodOrder() {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("goodorderlist", goodOrderService.getAllGoodOrder());
		
		return map;
	}
//	
	//查询当前用户的全部的订单
	//现在需要加入对应的订单明细的菜品总数  以及  订单明细前几两个菜名
	@RequestMapping("/usergoodorder")
	public Object getUserAllGoodOrder(HttpSession session) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");
		
		int userid = customerUser.getUser_id();
		
		System.out.println("当前用户查询userid"+userid);
		
		//当前用户的订单在加载时需要更新一下数据库
		goodOrderService.updateState(userid);
		
		map.put("usergoodorderlist", goodOrderService.getUserAllGoodOrders(userid));
		
		return map;
	}
	
	//查询当前用户的全部已经送达订单
	@RequestMapping("/userarrivedgoodorder/{user_ID}")
	public Object getUserArrivedGoodOrders(@PathVariable("user_ID")Integer user_ID) {
			
		Map<String,Object> map = new HashMap<String, Object>();
			
		map.put("userarrivedgoodorderlist", goodOrderService.getUserArrivedGoodOrders(user_ID));
			
		return map;
		
	}
	
	//查询当前用户全部支付订单
	@RequestMapping("/userpaidgoodorder/{user_ID}")
	public Object getUserPaidGoodOrders(@PathVariable("user_ID")Integer user_ID) {
			
		Map<String,Object> map = new HashMap<String, Object>();
			
		map.put("usergoodorderlist", goodOrderService.getUserPaidGoodOrders(user_ID));
			
		return map;
	}
	
	//查询当前用户已经取消订单
	@RequestMapping("/usercancelgoodorder/{user_ID}")
	public Object getUserCancelGoodOrders(@PathVariable("user_ID")Integer user_ID) {
			
		Map<String,Object> map = new HashMap<String, Object>();
			
		map.put("usergoodorderlist", goodOrderService.getUserCancelGoodOrders(user_ID));
			
		return map;
		
	}
	
	//根据订单号查询
	@RequestMapping("/getgoodorder/{order_id}")
	public Object getGoodOrder(@PathVariable("order_id")Integer order_id) {
			
		Map<String,Object> map = new HashMap<String, Object>();
			
		map.put("usergoodorder", goodOrderService.getGoodOrderById(order_id));
		
		//当前订单信息
		
		System.out.println(goodOrderService.getGoodOrderById(order_id).toString());
			
		return map;
		
	}
	
	//生成订单以及订单明细
	@RequestMapping("/creategoodorder")
	public Object insertGoodOrder(@RequestBody String goodOrderAndDetail, HttpSession session) {

		JSONObject jsonObject = JSON.parseObject(goodOrderAndDetail);
		
		//GoodOrder goodOrder = jsonObject.getObject("good_order", GoodOrder.class);
		GoodOrder goodOrder = JSON.parseObject(jsonObject.getString("good_order"), GoodOrder.class);
		List<GoodOrderDetail> goodOrderDetailList = JSON.parseArray(jsonObject.getString("good_order_detail_list"), GoodOrderDetail.class);
		
		//暂时不用
		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");
		goodOrder.setOrder_owner(customerUser.getUser_id());
		goodOrder.setOrder_owner_name(customerUser.getUser_name());
		
		boolean isGoodOrderInsert = goodOrderService.insertGoodOrder(goodOrder);
		
		int order_id = goodOrder.getOrder_id();
		
		for (GoodOrderDetail goodOrderDetail : goodOrderDetailList) {
			goodOrderDetail.setOrder_id(order_id);
		}
		
		System.out.println(goodOrderDetailList);
		
		boolean isGoodOrderDetailInsert = goodOrderDetailService.insertForeach(goodOrderDetailList);
		
		Map<String,Object> map = new HashMap<String, Object>();	
		
		
		map.put("isInsert", isGoodOrderDetailInsert);
		map.put("order", goodOrder);
			
		return map;
		
	}
	
	//订单更新(包括状态在内)
	@RequestMapping("/updategoodorder")
	public Object updateGoodOrder(@RequestBody GoodOrder goodOrder, HttpSession session) {
		
		System.out.println(goodOrder.getCarrier());
		
		CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");
		
		goodOrder.setOrder_owner(customerUser.getUser_id());
		goodOrder.setOrder_owner_name(customerUser.getUser_name());
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("updateAim", goodOrderService.updateGoodOrder(goodOrder));
		
		return map;
		
	}
	
	
	//取消订单
	@RequestMapping("/updategoodorderstatus/{order_id}")
	public Object updateGoodOrderStatus(@PathVariable("order_id") Integer order_id, HttpSession session) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		//写一个取消订单的
		map.put("updateAim", goodOrderService.updateGoodOrderState(order_id));
		
		return map;
	}
	//查看当天订单数量
	@RequestMapping("/getnumberbydate")
	public Object getNumberByDate(@RequestParam String date) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("goodordernum", goodOrderService.getNumberByDate(date));
		map.put("foodtotalsalenum", goodOrderDetailService.getSaleNumberByDate(date));
		map.put("foodsalenum", goodOrderDetailService.getFoodSaleNumber(date));
		
		return map;
	}
	
	@RequestMapping("/getnumberoftoday")
	public Object getNumberOfThisDay() {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("goodordernum", goodOrderService.getNumberToday());
		map.put("foodtotalsalenum", goodOrderDetailService.getSaleNumberToday());
		map.put("Sales",goodOrderService.getTotalSales());
		SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(System.currentTimeMillis());
		Calendar calendar = new GregorianCalendar();

		calendar.setTime(new Date());

		calendar.add(calendar.DATE,-1);

		String date2= formatter.format(calendar.getTime());

		map.put("yestodaygoodordernum", goodOrderService.getNumberByDate(date2));
		map.put("yestodayfoodtotalsalenum", goodOrderDetailService.getSaleNumberByDate(date2));
		map.put("yestodaysales", goodOrderService.getTotalSalesByDate(date2));
		
		return map;
	}
	/**
	 * 订单管理的时间间区间搜索
	 * url/goodorder/searchByTime
	 * @author shy
	 */
	@RequestMapping("/searchByTime")
	public Object searchOrderByTime(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("searchByTimeList", goodOrderService.getByTimeWithPage((int)map.get("pageNum"),(int)map.get("pageSize"),(String)map.get("time1"),(String)map.get("time2")));
		
		return rmap;
		
	}
	
	/**
	 * 订单管理全部显示
	 * url/goodorder/searchAllByPage
	 * @author shy
	 */
	@RequestMapping("/searchAllByPage")
	public Object searchAllByPage(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("searchAllByTimeList", goodOrderService.getAllWithPage((int)map.get("pageNum"),(int)map.get("pageSize")));
		
		return rmap;
		
	}
	
	/**
	 * 订单管理的批量删除
	 * url/goodorder/deleteByGroup
	 * @author shy
	 */
	@RequestMapping("/deleteByGroup")
	public Object deleteByGroup(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("deletByGroupResult", goodOrderService.deleteByGroup((List<Integer>)map.get("list")));
		
		return rmap;
		
	}
	@RequestMapping("/getdateandsalenum")
	public Object getDateAndSaleNum(@RequestBody Map<String,Object> map) {
				
		Map<String,Object> dmap = new HashMap<String, Object>();
		
		dmap=goodOrderService.getDateAndTotalSaleNum((String)map.get("start_date"),(String)map.get("end_date"));
				
		return dmap;
		
	}
	@RequestMapping("/getdateandordernum")
	public Object getDateAndOrderNum(@RequestBody Map<String,Object> map) {
				
		Map<String,Object> dmap = new HashMap<String, Object>();
		
		dmap=goodOrderService.getDateAndOrderNum((String)map.get("start_date"),(String)map.get("end_date"));
				
		return dmap;
		
	}
	@RequestMapping("/getdateandprice")
	public Object getDateAndPrice(@RequestBody Map<String,Object> map) {
				
		Map<String,Object> dmap = new HashMap<String, Object>();
		
		dmap=goodOrderService.getDateAndPrice((String)map.get("start_date"),(String)map.get("end_date"));
				
		return dmap;
		
	}	
	/**
	 * 订单管理的单个删除
	 *  url/goodorder/deleteById
	 * @author shy
	 */
	@RequestMapping("/deleteById")
	public Object deleteById(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("deleteResult", goodOrderService.deleteById((Integer)map.get("order_id")));
		
		return rmap;
		
	}
	
	/**
	 * 查看详情在上
	 * url/all/page
	 * @author shy
	 */
	@RequestMapping("/getDetail")
	public Object getDetail(@RequestBody Map<String,Object> map) {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		rmap.put("orderInfo", goodOrderService.getGoodOrderById((int)map.get("orderid")));
		rmap.put("detailList", goodOrderDetailService.getDetails((int)map.get("orderid")));
		
		return rmap;
		
	}
	
	/**
	 * 订单内容组合查询并分页显示查询结果
	 * url/goodorder/getDetail
	 * @author shy
	 */@RequestMapping("/getOrderByOrderInfo")
		public Object getOrderByOrderInfo(@RequestBody GoodOrder goodOrder,@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize) {
			
			Map<String,Object> rmap = new HashMap<String, Object>();
			
//			GoodOrder goodOrder = new GoodOrder();
//			goodOrder.setOrder_owner_name((String)map.get("order_owner_name"));
//			
//			if(map.get("order_id")==null || map.get("order_id").equals("")) {
//				goodOrder.setOrder_id(null);
//			}else {
//				goodOrder.setOrder_id((int)map.get("order_id"));
//			}
//			
//			if(map.get("status")==null || map.get("status").equals("")) {
//				goodOrder.setStatus(null);
//			}else {
//				goodOrder.setStatus((int)map.get("status"));
//			}			
			
			rmap.put("searchResult", goodOrderService.getByGoodOrderInfoWith(pageNum,pageSize, goodOrder));
			
			return rmap;
			
		}
	 
	 /**
		 * 后台修改订单  区别于前台
		 * url/goodorder/updateBack
		 * @author shy
		 */
	 @RequestMapping("/updateBack")
		public Object updateBack(@RequestBody GoodOrder goodOrder) {
			
			System.out.println(goodOrder.getOrder_owner_name());	
			
			Map<String,Object> map = new HashMap<String, Object>();
			
			map.put("updateBackResult", goodOrderService.updateGoodOrder(goodOrder));
			
			return map;
			
		}
	 
	 /**
		 * 订单内容组合查询并分页显示查询结果
		 * url/goodorder/getDetail
		 * @author shy
		 */@RequestMapping("/getOrderByUser")
			public Object getOrderByUser(@RequestBody GoodOrder goodOrder,@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize,HttpSession session) {
				
				Map<String,Object> rmap = new HashMap<String, Object>();
				
				CustomerUser customerUser = (CustomerUser) session.getAttribute("loginUser");

				//获取当前用户下的用户id
				int userid = customerUser.getUser_id();
				
				goodOrder.setOrder_owner(userid);
				
				rmap.put("searchResult", goodOrderService.getByGoodOrderInfoWith(pageNum,pageSize, goodOrder));
				
				return rmap;
				
			}
		 
}
